<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/20 0020
 * Time: 10:23
 */




namespace app\admin\controller;


use app\model\model\Admin_menu;
use app\home\model\BfAttachments;

use think\Controller;

class BaseController extends Controller
{
    public function _initialize()
    {
        //登录控制
        if(!session('admin_id')){
            $this->redirect('/admin/login/index.html');  //WEB_URL
            exit();
        }
    }

    protected function _getAuthMenu($uid = 1){
        if(empty($uid)){
            return false;
        }

        $user_model = new \app\model\model\Adminuser();
        $users = $user_model->getUserOne($uid);
        if(empty($users)){
            return false;
        }

        $role_model = new \app\model\model\Role();
        $roles = $role_model->getOneById($users['role']);
        if(empty($roles) || empty($roles['power'])){
            return false;
        }

        $menu_ids = explode(',', $roles['power']);
        if(empty($menu_ids)){
            return false;
        }

        $menu_model = new \app\model\model\Authmenu();
        $field = 'id,parent_id,name,url,sort,type';

        $menu_list = $menu_model->where([['id','in',$menu_ids]])->order(['type'=>'asc','sort'=>'asc'])->column($field);
        
        if(empty($menu_list)){
            return false;
        }
        $menu_list_arr = [
            1=>['name'=>'基础管理','menu'=>[]],
            2=>['name'=>'询盘管理','menu'=>[]],
            3=>['name'=>'会员管理','menu'=>[]],
            4=>['name'=>'系统管理','menu'=>[]],
        ];

        foreach ($menu_list as $val){
            if(empty($val['parent_id'])){
                $menu_list_arr[$val['type']]['menu'][$val['id']] = $val;
            } else {
                if(isset($menu_list_arr[$val['type']]['menu'][$val['parent_id']])){
                    $menu_list_arr[$val['type']]['menu'][$val['parent_id']]['sub'][$val['id']] = $val;
                }
            }
        }

        return $menu_list_arr;

    }

    protected function _accessPv(){
        $access_model = new  \app\model\model\Accesspv();

        $request = Request::instance();

        $params = [
            'user_id'       => session('admin_id'),
            'page'          => $request->url(true),
            'agent'         => $request->header('user-agent'),
            'ip'            => $request->ip(),
            'city'          => taobaoIP($request->ip()),
            'browser'       => getBrowser($request->header('user-agent')),
            'create_time'   => time(),
        ];
        $access_model->add($params);

        $access_model = new  \app\model\model\Accessip();
        $data = $access_model->getDetail(['ip'=>$params['ip'],'create_time'=>['EGT', strtotime(date('Y-m-d'))]]);
        if(empty($data)){
            $params = [
                'user_id'       => session('admin_id'),
                'agent'         => $request->header('user-agent'),
                'ip'            => $request->ip(),
                'city'          => taobaoIP($request->ip()),
                'browser'       => getBrowser($request->header('user-agent')),
                'create_time'   => time(),
            ];
            $access_model->add($params);
        }
    }
    
    public function _upload(){
        $files = $this -> request -> file();
        $post = $this -> request -> post();
        
        foreach($files as $f){
            $fname = $f -> validate(['size'=>1000000,'ext'=>'jpg,png,gif']) -> move('upload/images/') -> getSaveName();
            $flist[] = '/upload/images/'. str_replace('\\',"/",$fname);
            $data = [
                'subject' => $post['subject'],
                'path' => '/upload/images/'. str_replace('\\',"/",$fname),
                'flag' => 0,
                'create_time' => time()
            ];
        }
        
        $id = BfAttachments :: create($data);
        return $id;
    }
    //格式化函数
    public static function byte_format($size, $dec=2){
        $a = array("B", "KB", "MB", "GB", "TB", "PB");
        $pos = 0;
        while ($size >= 1024) {
            $size /= 1024;
            $pos++;
        }
        return round($size,$dec)." ".$a[$pos];
    }

}